﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Zhao.Common.Helper;
using Zhao.IServer.Field;
using Zhao.Models.AppletDto;
using Zhao.Models.DTO;
using Zhao.Models.Entity;

namespace Zhao.Arena.MiniProgram.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FieldsController : ControllerBase
    {
        private readonly IFieldsServer _fieldsServer;

        public FieldsController(IFieldsServer fieldsServer)
        {
            _fieldsServer = fieldsServer;
        }

        /// <summary>
        /// 获取球馆预约不同时间段的价格和场地预约信息
        /// </summary>
        /// <param name="tId"></param>
        /// <param name="venuId"></param>
        /// <param name="display"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("SessionPrice/{tId:int}/{display}/{venuId}")]
        [Authorize]
        public async Task<JsonResult> GetSessionPrice(Guid venuId,int tId, string display)
        {
            var result = new ApiResult<List<VenuePricingSimpleDTO>>() { Success = false, Message = "操作失败" };

            //如果没传递必要的参数直接返回
            if (venuId == default || tId == 0) return new JsonResult(result);

            if (!display.IsValidIsoDate())
            {
                display = DateTime.Today.ToString("yyyy-MM-dd");
            }

            Expressionable<Venues, Fields> expressionable
                = new Expressionable<Venues, Fields>();

            expressionable.And((a, b) => a.VenueID.Equals(venuId) && b.FieldTypeID == tId);

            var data = await _fieldsServer.SessionPriceSimpleQuery(expressionable.ToExpression(), display);

            result.Data = data;

            if (result.Data != null)
            {
                result.Success = true;
                result.Message = "操作成功";
            }

            return new JsonResult(result);
        }
    }
}
